﻿Imports System.Data.OleDb
Imports LecturerAppointmentSystem.UserDetails
Public Class BookAppointment
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lbl_TodaysDate.Text = Date.Today
        If Not IsPostBack Then
            ddl_Faculty.DataBind()
            setDatasource()
        End If
    End Sub

    Protected Sub AddBlankItem(ByVal sender As Object, ByVal e As EventArgs)
        ddl_Faculty.Items.Insert(0, New ListItem("Any", "0"))
    End Sub

    Protected Sub validateDate(ByVal sender As Object, ByVal e As EventArgs) Handles tb_Date.TextChanged
        Dim chosenDate As String = tb_Date.Text
        If Not chosenDate = "" Then
            If chosenDate < Date.Today Then
                lblDateError.Text = "Date has already passed. Please choose a later date."
                lblDateError.CssClass = "showControl"
            End If
        Else
            lblDateError.CssClass = "hideControl"
        End If
    End Sub

    Sub setDatasource()
        If Request.QueryString("date") = Nothing And Request.QueryString("faculty") = Nothing And Not Request.QueryString("name") = Nothing Then
            SearchMultiView.SetActiveView(SearchByNameView)
            SearchByNameGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByName"
            'SearchGridView.Visible = True
        ElseIf Request.QueryString("name") = Nothing And Request.QueryString("faculty") = Nothing And Not Request.QueryString("date") = Nothing Then
            SearchMultiView.SetActiveView(SearchByDateView)
            SearchByDateGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByDate"
            'SearchGridView.Visible = True
        ElseIf Request.QueryString("name") = Nothing And Request.QueryString("date") = Nothing And Not Request.QueryString("faculty") = Nothing Then
            SearchMultiView.SetActiveView(SearchByFacultyView)
            SearchByFacultyGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByFaculty"
            'SearchGridView.Visible = True
        ElseIf Request.QueryString("faculty") = Nothing And Not Request.QueryString("name") = Nothing And Not Request.QueryString("date") = Nothing Then
            SearchMultiView.SetActiveView(SearchByNameAndDateView)
            SearchByNameAndDateGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByNameAndDate"
            'SearchGridView.Visible = True
        ElseIf Request.QueryString("date") = Nothing And Not Request.QueryString("name") = Nothing And Not Request.QueryString("faculty") = Nothing Then
            SearchMultiView.SetActiveView(SearchByNameAndFacultyView)
            SearchByNameAndFacultyGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByNameAndFaculty"
            'SearchGridView.Visible = True
        ElseIf Request.QueryString("name") = Nothing And Not Request.QueryString("date") = Nothing And Not Request.QueryString("faculty") = Nothing Then
            SearchMultiView.SetActiveView(SearchByDateAndFacultyView)
            SearchByDateAndFacultyGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByDateAndFaculty"
            'SearchGridView.Visible = True
        ElseIf Not Request.QueryString("name") = Nothing And Not Request.QueryString("date") = Nothing And Not Request.QueryString("faculty") = Nothing Then
            SearchMultiView.SetActiveView(SearchByNameDateAndFacultyView)
            SearchByNameDateAndFacultyGridView.DataBind()
            'SearchGridView.DataSourceID = "SearchByNameDateAndFaculty"
            'SearchGridView.Visible = True
        ElseIf Not Request.QueryString("ID") = Nothing Then
            SearchMultiView.SetActiveView(SearchByUserIdView)
            SearchByUserIdGridView.DataBind()
       Else
            SearchMultiView.SetActiveView(DefaultView)
        End If
    End Sub

    Protected Sub btn_Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Search.Click
        If tb_Date.Text = Nothing And ddl_Faculty.SelectedValue = 0 And Not tb_Name.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?name=" & tb_Name.Text)
        ElseIf ddl_Faculty.SelectedValue = 0 And tb_Name.Text = Nothing And Not tb_Date.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?date=" & CType(tb_Date.Text, Date))
        ElseIf ddl_Faculty.SelectedValue > 0 And tb_Name.Text = Nothing And tb_Date.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?faculty=" & ddl_Faculty.SelectedValue)
        ElseIf ddl_Faculty.SelectedValue = 0 And Not tb_Name.Text = Nothing And Not tb_Date.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?name=" & tb_Name.Text & "date=" & tb_Date.Text)
        ElseIf ddl_Faculty.SelectedValue > 0 And Not tb_Name.Text = Nothing And tb_Date.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?name=" & tb_Name.Text & "faculty=" & ddl_Faculty.SelectedValue)
        ElseIf ddl_Faculty.SelectedValue > 0 And tb_Name.Text = Nothing And Not tb_Date.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?date=" & tb_Date.Text & "faculty=" & ddl_Faculty.SelectedValue)
        ElseIf ddl_Faculty.SelectedValue > 0 And Not tb_Name.Text = Nothing And Not tb_Date.Text = Nothing Then
            Response.Redirect("BookAppointment.aspx?name=" & tb_Name.Text & "date=" & tb_Date.Text & "faculty=" & ddl_Faculty.SelectedValue)

        End If
    End Sub

    Protected Sub btn_Book_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn_BookAppointment As Button = CType(sender, Button)
        Dim row As GridViewRow = TryCast(btn_BookAppointment.NamingContainer, GridViewRow)

        Dim appointmentId As Integer = CType(row.Cells(0).Text, Integer)
        Response.Redirect("AppointmentDetails.aspx?ID=" & appointmentId)
        'If (isAppointmentMarkedAsAvailable(timeSlot)) Then
        '    If isAppointmentBooked(Request.QueryString("Date"), timeSlot) Then
        '        CancelBookingPopup.Show()
        '    Else
        '        UnavailablePopup.Show()
        '    End If
        'Else
        '    AvailablePopup.Show()
        'End If
    End Sub
End Class